/**
 * 语音组件
 */
(function (exports) {
    var RECORD_STATUS_RECORD_SUCCESS = 2;
    exports.MicLayer = {
        init: function (btn, node) {
            var that = node;
            var cancelOrSend = false;
            var chatTime = 0;
            var animNode = null;
            var voiceFilename = null;
            var uploadFilename = null;
            var hasSendFileName = null;
            TouchUtil.setListeners(btn, {
                onTouchBegan: function (node, touch, event) {
                    if (animNode && animNode.getParent()) {
                        animNode.removeFromParent();
                    }
                    cancelOrSend = true;
                    animNode = playAnimScene(that, chat.res.AnimMicNode_json, cc.winSize.width / 2, cc.winSize.height / 2, true);
                    chatTime = getCurTimestampMills();
                    voiceFilename = getCurTimestamp() + "-" + gameData.uid + "-";
                    uploadFilename = voiceFilename;
                    voiceFilename += Math.floor(Math.random() * 100) + '.aac';
                    VoiceUtil.startRecord();
                },
                onTouchMoveIn: function (node, touch, event) {
                    if (!cancelOrSend) {
                        cancelOrSend = true;
                        animNode.removeFromParent();
                        animNode = playAnimScene(that, chat.res.AnimMicNode_json, cc.winSize.width / 2, cc.winSize.height / 2, true);
                        animNode.setPosition(that.getContentSize().width / 2, that.getContentSize().height / 2);
                    }
                },
                onTouchMoveOut: function (node, touch, event) {
                    if (cancelOrSend) {
                        cancelOrSend = false;
                        animNode.removeFromParent();
                        animNode = ccs.load(chat.res.ChatNotSendNode_json, "res/").node;
                        animNode.setPosition(cc.p(cc.winSize.width / 2, cc.winSize.height / 2));
                        that.addChild(animNode);

                        var voice_cancel = new cc.Sprite(chat.res.voice_cancel);
                        voice_cancel.setPosition(cc.p(cc.winSize.width / 2, cc.winSize.height / 2));
                        that.addChild(voice_cancel);
                        voice_cancel.runAction(cc.sequence(
                            cc.hide(),
                            cc.delayTime(0.4),
                            cc.show(),
                            cc.delayTime(1.2),
                            cc.removeSelf()
                        ))
                    }
                },
                onTouchEndedWithoutCheckTouchMe: function (node, touch, event) {
                    chatTime = getCurTimestampMills() - chatTime;
                    animNode.removeFromParent();
                    animNode = null;
                    if (cancelOrSend) {
                        if (chatTime > 1000) {
                            VoiceUtil.stopRecord();
                            if (window.recordinterval) {
                                clearInterval(window.recordinterval);
                                window.recordinterval = null;
                            }
                            var checkFunc = function () {
                                var recordStatus = VoiceUtil.getCurRecordStatus();
                                if (recordStatus == RECORD_STATUS_RECORD_SUCCESS) {
                                    var savedFilePath = VoiceUtil.getCurSavedVoiceFilePath();
                                    clearInterval(window.recordinterval);
                                    uploadFilename = uploadFilename + "" + (Math.floor(chatTime) + 500) + '.aac';
                                    //防止声音多次上传
                                    if (hasSendFileName) {
                                        if (uploadFilename.indexOf(hasSendFileName) > -1) {
                                            return;
                                        }
                                    }
                                    hasSendFileName = uploadFilename;
                                    http.uploadFileToOSS(savedFilePath, uploadFilename, function (url) {
                                        if (gameData.wanfatype == 'java') {
                                            network.send(3008, {room_id: gameData.roomId, type: "voice", content: url});
                                        } else {
                                            var urlbase64 = encodeURIComponent(url);
                                            var msg = JSON.stringify({
                                                roomid: mRoom.roomId,
                                                type: 'voice',
                                                voice: '',
                                                content: urlbase64,
                                                from: gameData.uid
                                            });
                                            network.wsData("Say/" + msg);
                                        }
                                    }, function () {
                                        // console.log("upload fail");
                                    });
                                }
                            };
                            window.recordinterval = setInterval(checkFunc, 32);
                        } else {
                            animNode = ccs.load(chat.res.ChatErrorNode_json, getCCSFileNamePath(chat.res.ChatErrorNode_json)).node;
                            animNode.setPosition(cc.p(cc.winSize.width / 2, cc.winSize.height / 2));
                            animNode.runAction(cc.sequence(cc.delayTime(1), cc.callFunc(function () {
                                animNode.removeFromParent();
                                animNode = null;
                            })));
                            that.addChild(animNode);
                            VoiceUtil.stopRecord();
                        }
                    }
                    else
                        VoiceUtil.stopRecord();
                }
            });
        }
    }

})(this);